#include <cxcore.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <math.h>
//#include <direct.h>
#include <iostream>

//the manual
#include "../face/faceson.h"

using namespace std;
IplImage* Kes(IplImage*,IplImage*,IplImage*);
int main(){
    CvCapture* capture = cvCaptureFromCAM(0);
    IplImage *frame = 0;
    IplImage* gri = cvCreateImage(cvSize(92,112), IPL_DEPTH_8U, 1);
    IplImage* res;
    IplImage* mo;
    IplImage* ycb;
    IplImage* gr;
    int i,j=0;
    bool first = true; 
    char b[10];
    cvNamedWindow( "Cam", 1 );
    cvNamedWindow( "Kes", 1 );
#ifdef unix
    system("mkdir dosya");
#else
    mkdir("dosya");
#endif
      if (capture){
          for(i=0;i<100;i++){
            if( !cvGrabFrame( capture ))
                break;
            frame = cvRetrieveFrame( capture );
            if(first){
            res = cvCreateImage(cvSize(frame->width,frame->height), IPL_DEPTH_8U, 3);
            mo = cvCreateImage(cvSize(frame->width,frame->height), IPL_DEPTH_8U, 1);
            ycb = cvCreateImage(cvSize(frame->width,frame->height), IPL_DEPTH_8U, 3);
            gr = cvCreateImage(cvSize(frame->width,frame->height), IPL_DEPTH_8U, 1);
            first=false;
            }
            res=Lmain(frame);
            cvCvtColor(res,ycb,CV_BGR2YCrCb);
            mo=Mmain(ycb);
            gr=Smain(res);
            gri=Kes(res,gr,mo);
            char name[100]="dosya/resim";
            sprintf(b,"%d",j);
            strcat(name,b);
            strcat(name,".pgm");
            j++;
     	   #ifdef WIN32
		    cvFlip( gri, 0, 0 );
		    gri->origin = 0;
		    #endif
           cvShowImage( "Cam", frame );
           cvShowImage( "Kes", gri );
            if( cvWaitKey( 5 ) >= 0 )
                break;
            cvEqualizeHist(gri,gri);
            cvSaveImage(name,gri);
          }
      }
    cvReleaseImage(&res);
    cvReleaseImage(&mo);
    cvReleaseImage(&ycb);
    cvReleaseImage(&gr);
    cvReleaseImage(&gri);
    cvDestroyWindow("Cam");
    cvDestroyWindow("Kes");
    return 0;

}
IplImage* Kes(IplImage* resim,IplImage* gr,IplImage* mo){
   int w=0,h=0;
   CvRect bndRect = cvRect(0,0,0,0);
   CvRect mRect = cvRect(0,0,0,0);
   CvMemStorage* storage = cvCreateMemStorage(0);
   CvSeq* contour = 0;
   CvSeq* contour1 = 0;
   cvFindContours( gr, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
   cvFindContours( mo, storage, &contour1, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
   CvPoint pt1, pt2;
   CvPoint ma1, ma2;
      CvPoint ptm1, ptm2;
   CvPoint mam1, mam2;
   ma1.x=1;
   ma2.x=1;
   ma1.y=1;
   ma2.y=1;
      mam1.x=1;
   mam2.x=1;
   mam1.y=1;
   mam2.y=1;
   for( ; contour1 != 0; contour1 = contour1->h_next ){
   mRect = cvBoundingRect(contour1, 0);
   ptm1.x = mRect.x;
   ptm1.y = mRect.y;
   ptm2.x = mRect.x + mRect.width;
   ptm2.y = mRect.y + mRect.height;
   if(abs(ptm1.x-ptm2.x)>abs(mam1.x-mam2.x)){
   mam1.x=ptm1.x;
   mam2.x=ptm2.x;
   w=mRect.width;
    
      mam1.y=ptm1.y;
      mam2.y=ptm2.y;
      h=mRect.height;
    
   }

}
 for( ; contour != 0; contour = contour->h_next ){
   bndRect = cvBoundingRect(contour, 0);
   pt1.x = bndRect.x;
   pt1.y = bndRect.y;
   pt2.x = bndRect.x + bndRect.width;
   pt2.y = bndRect.y + bndRect.height;
   
   if((((mam1.x>=(pt1.x-w/2) && mam1.x<=(pt2.x+w/2)) || (mam2.x<=(pt2.x+w/2) && mam2.x>=(pt1.x+w/2))) && (mam1.y>=(pt1.y-h/2) && mam2.y<=(pt2.y+h/2) )) && abs(pt1.x-pt2.x)>abs(ma1.x-ma2.x)){
   ma1.x=pt1.x;
   ma2.x=pt2.x;
            ma1.y=pt1.y;
            ma2.y=pt2.y;
       
   }

}  

   IplImage* crop = cvCreateImage(cvSize(abs(ma2.x-ma1.x),abs(ma2.y-ma1.y)), IPL_DEPTH_8U, 3);
   IplImage* gri = cvCreateImage(cvSize(92,112), IPL_DEPTH_8U, 1);
   IplImage* boyut = cvCreateImage(cvSize(92,112), IPL_DEPTH_8U, 3);

   if(abs(ma2.x-ma1.x)!=0 && abs(ma2.y-ma1.y)!=0){
   IplImage* ye;
   mRect = cvRect(ma1.x,ma1.y,abs(ma2.x-ma1.x),abs(ma2.y-ma1.y));
   cvSetImageROI(resim,mRect);
   cvCopy(resim,crop,0);
   
   ye=resz(crop);
   cvResize(ye,boyut,CV_INTER_LINEAR);
   cvCvtColor(boyut,gri,CV_BGR2GRAY);

   cvReleaseImage(&ye);
   }
    
    cvReleaseImage(&crop);
//    cvReleaseImage(&gri);
    cvReleaseImage(&boyut);
    cvFree(&storage);
    return gri;

}
